﻿using SimpleInOutControl.Common;
using SimpleInOutControl.DBModels;

namespace SimpleInOutControl.Services
{
    /// <summary>
    /// 系统参数服务
    /// </summary>
    public class SystemParameterService: BaseService
    {
        public bool AddData(SystemParameter dto)
        {
            using (var client = DataBaseHelper.GetSqlSugarClient())
            {
                var count = client.Insertable(dto).ExecuteCommand(); ;
                return count > 0;
            }
        }
        public bool UpdateData(SystemParameter dto)
        {
            dto.ID = 1;
            using (var client = DataBaseHelper.GetSqlSugarClient())
            {
                var count = client.Updateable(dto).IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true).ExecuteCommand();
                return count > 0;
            }
        }

        public bool DeleteData(int id)
        {
            //系统管理员不允许删除
            if (id == 1)
            {
                return false;
            }
            using (var client = DataBaseHelper.GetSqlSugarClient())
            {
                var obj = client.Queryable<SystemParameter>().Where(u => u.ID == id).First();
                if (obj == null)
                {
                    return false;
                }
                var count = client.Deleteable<SystemParameter>(obj).ExecuteCommand();
                return count > 0;
            }
        }

        public SystemParameter GetData(int id)
        {
            using (var client = DataBaseHelper.GetSqlSugarClient())
            {
                var user = client.Queryable<SystemParameter>().Where(u => u.ID == id).First();
                return user;
            }
        }

        public List<SystemParameter> GetPageData(PagingRequest<UserInfo> dto)
        {
            using (var client = DataBaseHelper.GetSqlSugarClient())
            {
                var users = client.Queryable<SystemParameter>().ToList();
                return users;
            }
        }
    }
}
